From 85bafd90471ff60561051f1047c3ccb24aefb1ef Mon Sep 17 00:00:00 2001 From: tsteven4 <13596209+tsteven4@users.noreply.github.com> Date: Fri, 14 Aug 2020 16:41:03 -0600 Subject: [PATCH] =?utf8?q?compile=20in=20gpsbabel=20translations=20and=20m?= =?utf8?q?ap=20on=20non=20mac=20unix=20distribution=E2=80=A6=20(#622)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit * compile in gpsbabel translations and map on non mac unix distributions by default. This eliminates the need to install gpsbabelfe_*.qm, gpsbabel_*.qm and gmapbase.html on the system. Note that the Qt supplied translations must still be installed on the system. The Qt translations may be in a package like qttranslations5-l10n or qt5-qttranslations. * simplify optional resources using std methods. * discuss dependencies in INSTALL. --- INSTALL | 8 ++++++++ gui/app.pro | 17 ++++++++++++++--- gui/mainwindow.cc | 28 ++++++++++++++++++++++------ gui/mainwindow.h | 1 - gui/map.cc | 13 +++++++++---- gui/map.qrc | 5 +++++ gui/translations.qrc | 16 ++++++++++++++++ 7 files changed, 74 insertions(+), 14 deletions(-) create mode 100644 gui/map.qrc create mode 100644 gui/translations.qrc diff --git a/INSTALL b/INSTALL index 06faf40b7..9f7da6c31 100644 --- a/INSTALL +++ b/INSTALL @@ -57,6 +57,14 @@ unix-gui: Build the graphical user interface and collect the components for On macOS an app bundle will be created at gui/GPSBabelFE.app and an apple disk image will be created at gui/GPSBabelFE.dmg. +Dependencies: +On non-macOS unix builds by default we now compile in the gpsbabel generated +translation files, i.e. gpsbabelfe_*.qm, gpsbabel_*.qm, as well as +gmapbase.html. When compiled in these files do not need to be distributed. +These are used by the GUI. Additional translation files from Qt will also be +used if they are found. They may be in a package such as qttranslations5-l10n +or qt5-qttranslations. + Windows builds: Two build methods are supported with MSVC tools. 1. "qmake -tp vc" will create a visual studio project that can be built with msbuild. diff --git a/gui/app.pro b/gui/app.pro index 4daefde48..bf359612a 100755 --- a/gui/app.pro +++ b/gui/app.pro @@ -18,7 +18,7 @@ qtHaveModule(webenginewidgets) { QT += webenginewidgets webchannel DEFINES += HAVE_WEBENGINE } else { - QT += webkit webkitwidgets + QT += webkit webkitwidgets } unix:DESTDIR = objects @@ -38,10 +38,10 @@ unix { UI_DIR = tmp -RESOURCES = app.qrc +RESOURCES = app.qrc RC_FILE = app.rc -win32 { +win32 { TARGET=GPSBabelFE } win32-g++ { @@ -125,6 +125,17 @@ TRANSLATIONS += gpsbabelfe_fr.ts TRANSLATIONS += gpsbabelfe_hu.ts TRANSLATIONS += gpsbabelfe_it.ts +unix:!mac { + !defined(EMBED_TRANSLATIONS, var):EMBED_TRANSLATIONS = on + !defined(EMBED_MAP, var):EMBED_MAP = on +} +equals(EMBED_TRANSLATIONS, on) { + RESOURCES += translations.qrc +} +equals(EMBED_MAP, on) { + RESOURCES += map.qrc +} + macx|linux{ package.commands = QMAKE=$(QMAKE) ./package_app package.depends = $(TARGET) diff --git a/gui/mainwindow.cc b/gui/mainwindow.cc index 22b2b449c..52bd16b64 100644 --- a/gui/mainwindow.cc +++ b/gui/mainwindow.cc @@ -26,11 +26,14 @@ #include // for QEvent (& QEvent::LanguageChange, QEvent::LocaleChange) #include // for QFile #include // for QFileInfo +#include // for QLibraryInfo, QLibraryInfo::TranslationsPath #include // for QLocale #include // for QMimeData #include // for QProcess, QProcess::NotRunning #include // for QRegExp #include // for QSettings +#include // for QString +#include // for QStringList #include // for QTemporaryFile #include // for QTime #include // for QUrl @@ -219,9 +222,6 @@ MainWindow::MainWindow(QWidget* parent): QMainWindow(parent) ui_.outputWindow->setReadOnly(true); - langPath_ = QApplication::applicationDirPath(); - langPath_.append("/translations/"); - // Start up in the current system language. loadLanguage(QLocale::system().name()); loadFormats(); @@ -311,9 +311,25 @@ void MainWindow::switchTranslator(QTranslator& translator, const QString& filena // remove the old translator qApp->removeTranslator(&translator); - // load the new translator - if (translator.load(filename, langPath_)) { - qApp->installTranslator(&translator); + // Set a list of locations to search for the translation file. + // 1. In the file system in the translations directory relative to the + // location of the executable. + // 2. In the Qt resource system under the translations path. This is useful + // if the resource was compiled into the executable. + // 3. In the translations path for Qt. This is useful to find translations + // included with Qt. + const QStringList directories = { + QApplication::applicationDirPath() + "/translations", + ":/translations", + QLibraryInfo::location(QLibraryInfo::TranslationsPath) + }; + + // Load the new translator. + for (const auto& directory : directories) { + if (translator.load(filename, directory)) { + qApp->installTranslator(&translator); + break; + } } } diff --git a/gui/mainwindow.h b/gui/mainwindow.h index 3fd107fbf..b6de39944 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -70,7 +70,6 @@ private: QTranslator translatorCore_; // translation for the core application. QTranslator translatorQt_; // translations for Qt. QString currLang_; // currently loaded language. - QString langPath_; // Absolute path of language files. private: void loadFormats(); diff --git a/gui/map.cc b/gui/map.cc index a55c752e3..aac5d5729 100644 --- a/gui/map.cc +++ b/gui/map.cc @@ -90,13 +90,18 @@ Map::Map(QWidget* parent, connect(mclicker, SIGNAL(logTime(QString)), this, SLOT(logTime(QString))); #endif + // We search the following locations: + // 1. In the file system in the same directory as the executable. + // 2. In the Qt resource system. This is useful if the resource was compiled + // into the executable. QString baseFile = QApplication::applicationDirPath() + "/gmapbase.html"; - if (!QFile(baseFile).exists()) { + if (QFile(baseFile).exists()) { + this->load(QUrl::fromLocalFile(baseFile)); + } else if (QFile(":/gmapbase.html").exists()) { + this->load(QUrl("qrc:///gmapbase.html")); + } else { QMessageBox::critical(nullptr, appName, tr("Missing \"gmapbase.html\" file. Check installation")); - } else { - QString urlStr = "file:///" + baseFile; - this->load(QUrl(urlStr)); } #ifdef DEBUG_JS_GENERATION diff --git a/gui/map.qrc b/gui/map.qrc new file mode 100644 index 000000000..f683ddac5 --- /dev/null +++ b/gui/map.qrc @@ -0,0 +1,5 @@ + + + gmapbase.html + + diff --git a/gui/translations.qrc b/gui/translations.qrc new file mode 100644 index 000000000..582eeca08 --- /dev/null +++ b/gui/translations.qrc @@ -0,0 +1,16 @@ + + + coretool/gpsbabel_de.qm + coretool/gpsbabel_es.qm + coretool/gpsbabel_fr.qm + coretool/gpsbabel_hu.qm + coretool/gpsbabel_it.qm + coretool/gpsbabel_ru.qm + gpsbabelfe_de.qm + gpsbabelfe_es.qm + gpsbabelfe_fr.qm + gpsbabelfe_hu.qm + gpsbabelfe_it.qm + gpsbabelfe_ru.qm + + -- 2.30.2